﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.DAL.DataSetApplyDetailsTableAdapters;
using Announcement.Model;

namespace Announcement
{
    public partial class GiftShift : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApplyTable at = (ApplyTable)Session["ApplyTable"];
                string cCode = at.ContractCode;
                string applyer = at.Applicant;
                string houseLocal = at.HouseLocated;
                DateTime applyDate = (DateTime)at.ApplyDate;
                string giftCode = at.GiftCode;
                string shifter = at.Shifter;
                string gifter = at.Gifter;

                labCCode.Text = cCode;
                labName.Text = applyer;
                labLocal.Text = houseLocal;
                labApplyDate.Text = applyDate.ToString("yyyy年MM月dd日");
                labGiftCode.Text = giftCode;
                labShifter.Text = shifter;
                labGifter.Text = gifter;



                if (Request.QueryString["isPrn"] == "true")
                {
                    btnSave.Visible = false;
                    btnPrn.Enabled = true;
                    txtGiftCode.Visible = false;
                    txtShifter.Visible = false;
                    txtGifter.Visible = false;
                    labGiftCode.Visible = true;
                    labShifter.Visible = true;
                    labGifter.Visible = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //取得Session中申请表数据
            ApplyTable at = (ApplyTable)Session["ApplyTable"];

            //取得表单数据
            at.GiftCode = txtGiftCode.Text;
            at.Shifter = txtShifter.Text;
            at.Gifter = txtGifter.Text;

            //控制控件的显示
            txtGiftCode.Visible = false;
            txtShifter.Visible = false;
            txtGifter.Visible = false;

            labGiftCode.Text = at.GiftCode;
            labShifter.Text = at.Shifter;
            labGifter.Text = at.Gifter;

            labGiftCode.Visible = true;
            labShifter.Visible = true;
            labGifter.Visible = true;

            btnPrn.Enabled = true;
            btnSave.Enabled = false;

            //保存申请表到数据库
            var applyDetailsRow = new T_ApplyTableTableAdapter();
            //如果重复保存则返回
            if (applyDetailsRow.GetDataByContractCode(at.ContractCode).Rows.Count >0)
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "已存在", @"alert('" + "该公告已存在,无需重复保存" + "');", true);
                return;
            }
            applyDetailsRow.Insert(at.ContractCode,at.Applicant,at.Legal,at.CardTpye,at.CardNum,at.Agents,at.Phone,
                at.CardType2,at.CardNum2,at.HouseLocated,at.HouseNum,at.ApplyType,at.ApplyDate,at.IsPay,at.Money,
                at.GiftCode,at.Shifter,at.Gifter);

            
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["AdminUserName"] = null;
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
